//
//  ShiXunViewController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/3.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "ShiXunViewController.h"
#import "SYShiXunHeaderView.h"
#import "SYWanChengTableViewCell.h"
#import "SYChuangJianView.h"
#import "SYChengYuanDetailsView.h"
#import "SYChengYuanDetailsView.h"
#import "SYSXAlterView.h"


@interface ShiXunViewController () <UITableViewDelegate,UITableViewDataSource,SYWanChengTableViewCellDelegate>

@property (strong, nonatomic) SYTopView *topView;
@property (nonatomic, strong) UIButton *addBtn;//添加按钮
@property (nonatomic, strong) UITableView *curTableView;
@property (nonatomic, strong) SYChuangJianView *createView;//创建
@property (nonatomic, strong) SYChengYuanDetailsView *cyDetailsView;//完成
@property (nonatomic, strong) SYSXAlterView *alterView;

@end

@implementation ShiXunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    [self initSubView];
    
}

#pragma mark - tab
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 223+40+40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYWanChengTableViewCell *wCell =[tableView dequeueReusableCellWithIdentifier:@"SYWanChengTableViewCell" forIndexPath:indexPath];
    UIColor *color = nil;
    if (indexPath.row%2) {
        color = [UIColor whiteColor];
        
    } else {
        color = [UIColor colorWithHexString:@"F8F8F8"];
    }
    wCell.selectionStyle = UITableViewCellSelectionStyleNone;
    wCell.delegate = self;
    [wCell incomingDataInteger:indexPath.section backColor:color];
    return wCell;
   
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    SYShiXunHeaderView *headerView = [[SYShiXunHeaderView alloc] init];
    [view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(view).offset(14);
        make.right.equalTo(view).offset(-14);
        make.bottom.equalTo(view.mas_bottom).offset(0);
    }];
    if (section == 0) {
        [headerView shixunHeaderViewType:SYShiXunHeaderViewTypeXiuGai index:0];
    } else {
        [headerView shixunHeaderViewType:SYShiXunHeaderViewTypeDefault index:1];
    }
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    UIView *topView = [UIView new];
    topView.backgroundColor = [UIColor colorWithHexString:@"F8F8F8"];
    [view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.left.equalTo(view.mas_left).offset(14);
        make.right.equalTo(view.mas_right).offset(-14);
        make.height.mas_equalTo(20);
    }];
    return view;
}



//添加小组
- (void)addBtnClick {
    _createView = [[SYChuangJianView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
    [self.view addSubview:_createView];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.createView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
    }];
    
}

#pragma mark - cell得代理
//代理
- (void)selectTableViewCellType:(SYWanChengTableViewCellType)type {
    if (type == SYWanChengTableViewCellTypeChaKan) {//查看
        _cyDetailsView = [[SYChengYuanDetailsView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
        [self.view addSubview:_cyDetailsView];
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:kAnimate animations:^{
            weakSelf.cyDetailsView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
        }];
    } else {//移出
        _alterView = [[NSBundle mainBundle] loadNibNamed:@"SYSXAlterView" owner:nil options:nil].firstObject;
        [_alterView showKaoShiBanJi:_alterView dataModel:nil];
        
    }
}

#pragma mark - set
- (void)initSubView {
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
    
    [self.view addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(25);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(40);
    }];
    //表
    [self.view addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(0);
        make.left.equalTo(self.view).offset(14);
        make.right.equalTo(self.view.mas_right).offset(-14);
        make.bottom.equalTo(self.view.mas_bottom).offset(-14);
    }];
}


- (SYTopView *)topView {
    if (!_topView) {
        _topView = [[SYTopView alloc] init];
        _topView.titleLab.text = @"师训";
    }
    return _topView;
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_addBtn setImage:[UIImage imageNamed:@"add_teach"] forState:UIControlStateNormal];
        //        _addBtn.hidden = YES;
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_addBtn setTitleColor:TITLECOLOR forState:0];
        [_addBtn setTitle:@" 创建小组" forState:UIControlStateNormal];
    }
    return _addBtn;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        [_curTableView registerCellWithClassNames:@[@"SYWanChengTableViewCell"]];
    }
    return _curTableView;
}


@end
